<?php
/**
 * Created by PhpStorm.
 * User: lidan
 * Date: 2018/8/28
 * Time: 下午3:32
 */
namespace frame;

use twig\twig;
class Views
{
    protected $twig;
    protected $data = array();

    public function __construct($path) {
        $loader = new \Twig_Loader_Filesystem(ROOT_PATH . '/app/' . $path .'/views');
        $this->twig = new \Twig_Environment($loader, array(
            // 'cache' => FRAME_PATH . 'cache', // 注释表示不用缓存
            'debug' => false // 开启调试模式
        ));
    }

    /*
     * 设置模板数据
     * 2中设置方法
     * 1）key value方式：assign('key', 'value')
     * 2）数组方式：assign(array('key' => 'value'))
     */
    public function assign($var, $value = NULL) {
        if(is_array($var)) {
            $this->data = array_merge($this->data, $var);
        }else {
            $this->data[$var] = $value;
        }
    }

    public function display($template) {
        $template = $this->twig->loadTemplate($template . '.html');
        $template->display($this->data);
    }

}